﻿using System;
using System.Windows.Forms;
using PMS.Resources;
using System.Threading;
using System.Globalization;
using System.Collections.Generic;
using System.Configuration;
using PMS.Utils;
using System.Management;
using System.Data;
using PMS.BLL;

namespace PMS
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //ReportBO rp = new ReportBO();
            //DataSet ds = rp.BaoCaoXuatNhapTonReport("");
            //ds.WriteXmlSchema(@"..\..\..\XDS Files\BaoCaoXuatNhapTonXSD.xsd");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);
            try
            {
                string culture = ConfigurationManager.AppSettings["culture"];
                if (string.IsNullOrEmpty(culture))
                {
                    culture = "vi-VN";
                }
                CultureInfo cultureInfo = new System.Globalization.CultureInfo(culture);
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            try
            {
                if (ShowLogin() == DialogResult.OK)
                    Application.Run(new Main());
                //KO can phai dang ky
                //RegisterInfo info = Register.ReadRegisterInfoFromFile();
                //if (info != null && GetProcessorID().Equals(CryptorEngine.Decrypt(info.Key, true)))
                //{
                //    if (ShowLogin() == DialogResult.OK)
                //        Application.Run(new Main());
                //    else Application.Exit();
                //}
                //else
                //{
                //    RegisterForm regisform = new RegisterForm();
                //    if (regisform.ShowDialog() == DialogResult.OK)
                //    {
                //        if (ShowLogin() == DialogResult.OK)
                //            Application.Run(new Main());
                //        else Application.Exit();
                //    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void RegisterMessageBox()
        {
            MessageBoxManager.Unregister();
            //Set button text from resources
            MessageBoxManager.Cancel = FormResources.GetString("MessageBox.Cancel");
            MessageBoxManager.Retry = FormResources.GetString("MessageBox.Retry");
            MessageBoxManager.Ignore = FormResources.GetString("MessageBox.Ignore");
            MessageBoxManager.Abort = FormResources.GetString("MessageBox.Abort");
            MessageBoxManager.Yes = FormResources.GetString("MessageBox.Yes");
            MessageBoxManager.No = FormResources.GetString("MessageBox.No");
            MessageBoxManager.OK = FormResources.GetString("MessageBox.Ok");
            //Register manager
            MessageBoxManager.Register();
        }

        public static void ApplyCulture()
        {
            RegisterMessageBox();

            string symbol = ConfigurationConstant.SYS_DECIMAL_SYMBOL;
            CultureInfo cultureInfo = new System.Globalization.CultureInfo(Thread.CurrentThread.CurrentUICulture.IetfLanguageTag);

            NumberFormatInfo nfi = cultureInfo.NumberFormat;
            if (",".Equals(symbol))
            {
                nfi.NumberDecimalSeparator = ",";
                nfi.NumberGroupSeparator = ".";
            }
            else
            {
                nfi.NumberDecimalSeparator = ".";
                nfi.NumberGroupSeparator = ",";
            }
            cultureInfo.NumberFormat = nfi;

            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }

        private static DialogResult ShowLogin()
        {
            var login = new LoginForm();
            DialogResult result = System.Windows.Forms.DialogResult.Cancel;
            if (login != null)
            {
                result = login.ShowDialog();
                if (result != System.Windows.Forms.DialogResult.OK)
                {
                    Application.Exit();
                }
                else
                {
                    ApplyCulture();
                }
            }

            return result;
        }
        public static string GetProcessorID()
        {
            string sProcessorID = "";
            string sQuery = "SELECT ProcessorId FROM Win32_Processor";

            ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);

            ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();

            foreach (ManagementObject oManagementObject in oCollection)
            {

                sProcessorID = (string)oManagementObject["ProcessorId"];

            }

            return (sProcessorID);

        }
    }
}
